#!/usr/bin/env bash
set -Eeo pipefail

USER="$(id -u)"

create_directory_if_not_exists() {
    local dir="$1"
    if [ ! -e "$dir" ]; then
        mkdir "$dir"
    fi
}

create_directory_if_not_exists "/var/lib/tendis/db"
create_directory_if_not_exists "/var/lib/tendis/dump"
create_directory_if_not_exists "/var/lib/tendis/log"

if [ ! -e /usr/local/etc/tendis/extra.conf ]; then
        cp /usr/local/etc/tendisplus-extra.conf.sample /usr/local/etc/tendis/extra.conf
fi

if [ "$USER" -eq 0 ]; then
        chown -R tendis:tendis /var/lib/tendis /usr/local/etc/tendis
fi

exec gosu tendis "$@"
